package com.guoli.print;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;


public class TestPrintE {
	
	public static void main(String[] args) throws IOException {
		
		String strs[] = getPicString(new String[]{"车型","数量"},"宋体",60);
		
		for(String str : strs){
			System.out.println(str);
		}
		
//		jFrame.setSize(300, 300);
//		jFrame.setLocation(50, 50);
//		Graphics g2d = jFrame.getGraphics();
//		g2d.drawRect(5, 5, 200, 200);
//		g2d.dispose();
	
		
//		jFrame.setVisible(true);
	}
	public static String[] getPicString(String[] text, String fontName, int fontSize) throws IOException{
//		int width = (int)(2.1*fontSize);
//		int height = fontSize;
		int width = 130;
		int height = 60;
		String[] picString = new String[text.length];
		Font font = new Font(fontName, Font.BOLD, fontSize);
		for(int i=0;i<text.length;i++){
			BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//			File file = new File("F:\\java\\doc\\x"+i+".jpg");
			Graphics2D graphics2d = (Graphics2D) bi.getGraphics();
			graphics2d.setColor(Color.WHITE);
			graphics2d.fillRect(0, 0, width, height);
			graphics2d.setColor(Color.BLACK);
//			graphics2d.translate(0, -fontSize/8);
			graphics2d.setFont(font);
			graphics2d.drawRect(1, 1, width-3, height-3);
			graphics2d.drawString(text[i], 0, fontSize);
//			ImageIO.write(bi, "jpg", file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ImageIO.write(bi, "jpg", bos);
			byte[] picByte = bos.toByteArray();
			picString[i] = "";
			for(int j=0;j<picByte.length;j++){
				String str16 = Integer.toHexString(picByte[j] & 0xff);
				str16 = str16.length()==1 ? "0"+str16 : str16;
				picString[i]+=str16;
			}
			
//			picString[i] = Arrays.toString(picByte);
		}
		return picString;
	}
	
}
